<?php
/**
 * Created by PhpStorm.
 * User: 17208
 * Date: 2020/10/16
 * Time: 17:23
 */
namespace app\model;
use extend\Config;
use Medoo\Medoo;

class Base
{
    /** @var Medoo 数据库句柄 */
    private static $db;
    /** @var string 表名 */
    private static $table;
    /** @var int 数据状态 */
    private static $state_on = 1;
    private static $state_off = 0;

    public static function getDb(){
        if(!self::$db){
            $config = Config::get('db');
            self::$db = new Medoo($config);
        }
        return self::$db;
    }
    public static function select($columns,$where ){
        $where['state'] = self::$state_on;
        return (self::getDb())->select(static::$table,$columns,$where);
    }
    public static function insert($data){
        $data = array_merge([
            'state'=>self::$state_on,
            'create_time'=>time(),
            'update_time'=>time(),
        ],$data);
        return self::getDb()->insert(static::$table,$data);
    }
    public static function update( $data, $where){
        $where['state'] = self::$state_on;
        $data['update_time'] = time();
        return self::getDb()->update(static::$table, $data, $where);
    }
    public static function delete($where){
        $where['state'] = self::$state_on;
        $data = [
            'state'=>self::$state_off,
            'update_time'=>time(),
        ];
        return self::getDb()->update(static::$table, $data, $where);
    }
    public static function get($columns,$where ){
        $where['state'] = self::$state_on;
        return self::getDb()->get(static::$table,$columns,$where);
    }
    public static function log(){
        return self::getDb()->log();
    }
    public static function action($actions){
        return self::getDb()->action($actions);
    }
}